package com.sts.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sts.entity.Role;
import com.sts.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AuthMapper extends BaseMapper<User> {
    //User 相关（BaseMapper 已提供 insert、exists 等）
    //因为不在user表所有用
    //根据角色编码查询角色
    @Select("SELECT id, role_code as code, role_name as name FROM role WHERE role_code = #{code}")
    Role findRoleByCode(String customer);
    //插入用户-角色关联
    @Insert("INSERT INTO user_role(user_id, role_id) VALUES (#{userId}, #{roleId})")
    void insertUserRole(@Param("userId") Long userId, @Param("roleId") Long roleId);
    @Select("SELECT r.id, r.role_code, r.role_name FROM role r JOIN user_role ur ON r.id = ur.role_id WHERE ur.user_id = #{userId}")
    List<Role> findRolesByUserId(Long userId);
    @Select("SELECT r.role_code FROM role r JOIN user_role ur ON r.id = ur.role_id WHERE ur.user_id = #{userId}")
    List<String> findRoleCodesByUserId(Long userId);
}
